﻿package cz.dadul.adventure.controller
{
	/**
	 * ...
	 * @author Dadul
	 */
	import flash.display.*;
	import flash.events.*;
	import cz.dadul.adventure.model.*;
	import cz.dadul.adventure.view.*;
	import cz.dadul.adventure.model.events.*;
	 
	 public class PlayerController extends EventDispatcher
	{
		public static const EVENT_PLAYER_MOVED:String = "player_moved";
		
		// Constants
		// Singleton instance
		private static var instance:PlayerController;

		private var _player:Player;
		
		public function PlayerController(p_key:SingletonBlocker):void{
			// this shouldn't be necessary unless they fake out the compiler:
			 if (p_key == null) {
				throw new Error("Error: Instantiation failed: Use PlayerController.getInstance() instead of new.");
			  }
			  init();
		}

			
		/**
		 * PRIVATE
		 */
		private function init():void { }

		
		private function onPlayerMoved(e:Event):void {
			dispatchEvent(new DataHolderEvent(EVENT_PLAYER_MOVED, this._player));			
		}
		
		/**
		 * PUBLIC
		 */
		
		public function initPath():void {
			/**
			 * TODO: Initialize path for layer controller 
			 */
		}
				
				
		
		/**
		 * SETTERS / GETTERS
		 */
	   
		public function set player(p:Player):void {
			this._player = p;
			
			// Add event listeners
			this._player.addEventListener("playerMoved", onPlayerMoved);
			
		}
		public function get player():Player{
			return this._player;
		}				
				
		
		
		/**
		 * STATIC
		 */
		public static function getInstance():PlayerController {
         if (instance == null) {
            instance = new PlayerController(new SingletonBlocker());
          }
         return instance;
		}

	}
	
}

internal class SingletonBlocker {}